<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:">
      <el-input v-model="userInfo.password" disabled type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hSubmit">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'

export default {
  name: 'LoginSetting',
  data() {
    return {
      id: this.$route.query.id, // 查询路径获取id
      // 用户名称 - 密码
      userInfo: {
        username: '',
        password: ''
      }
    }
  },
  created() {
    this.loadUserDetailById() // 重新渲染
  },
  methods: {
    // 查询用户信息
    async loadUserDetailById() {
      try {
        const { data: res } = await getUserDetailById(this.id)
        this.userInfo = res.data
        // console.log('getUserDetailById', res)
      // 保存数据
      } catch (err) {
        console.log('getUserDetailById', err)
      }
    },
    // 修改用户信息
    async hSubmit() {
      try {
        await saveUserDetailById(this.userInfo)
        this.$message.success('修改成功')
        this.$router.push('/employees')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

